import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import { getUserInfoApi } from '@/api/index'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: '', // 保存token
    userInfo: {} // 保存用户的基本信息
  },
  getters: {
    username (state) {
      return state.userInfo.username
    },
    nickname (state) {
      return state.userInfo.nickname
    },
    user_pic (state) {
      return state.userInfo.user_pic
    }
  },
  mutations: {
    updateToken (state, newToken) {
      state.token = newToken
    },
    updateUserInfo (state, val) {
      state.userInfo = val
    }
  },
  actions: {
    // 获取用户基本信息
    async getUSerInfo ({ commit }) {
      const { data: res } = await getUserInfoApi()
      commit('updateUserInfo', res.data)
    }
  },
  modules: {},
  plugins: [createPersistedState()] // 持久化插件
})
